#
#Install site packages pyasn1-0.0.6a,  pysnmp-4.1.7a (or later), pycrypto-2.0.1 (? not sure)
#http://pysnmp.sourceforge.net/download.html. Tar files also provided in /tester/snmp.
#Copy GENESYS-SML-MIB-G7.py (provided in tester/snmp) to <pysnmp install folder>\pysnmp\v4\smi\mibs\instances
#
#If you got a new version of GENESYS-SML-MIB-G7.txt you'll need to make a mib in python format.
#Follow these steps:
#on LINUX 32 machine
#goto /home/masha/python/LINUX/bin
#copy new GENESYS-SML-MIB-G7.txt there. 
#Edit this file: add "IpAddress FROM SNMPv2-SMI" after "TimeTicks FROM SNMPv2-SMI"
#run "smidump GENESYS-SML-MIB-G7.txt | libsmi2pysnmp > GENESYS-SML-MIB-G7.py"
#copy GENESYS-SML-MIB-G7.py to <pysnmp install folder>\pysnmp\v4\smi\mibs\instances

#These test examples are for 'Application testing with SCS'
#All snmp requests are made on behalf of server object MasterAgent. Global name
#for this object is "SNMPMA" as configured in my Init file:
#  ApplicationUnderTheTest("SNMPMA", "MasterAgent", CfgMasterAgent1, ScsMasterAgent1, None, None, r"", 1)

#Working with tables. For more convenient access to the table fields tables defined in the mib
#could be mapped to python structs, see file ma_tables.py file. 
#Currently implemented table fields is gServersTableFields (only), 
#Feel free to add other tables in this file, just let me know.

#Note: 'testValue' statements in the tests are just examples, expected values mean nothing, use
#correct values for testing.





from ma_tables import * # to get access to snmp structs

def test1():
  """Example of Get, Set, GetNext, GetTableView"""
  CSDBID = (99,) # DBID if ConfigServer

  #Retrieve gServerId OID
  gServerId_ID = SNMPMA.GetOID('gServerId')

  #Get gServerId
  #Expected result - No SNMP Object
  result = SNMPMA.Get(gServerId_ID)
  SNMPMA.testValueAndType(result.value, Null("")) #Null means No SNMP Object. There won't be any error

  #Set gServerId
  #Expected result noAccess(6)
  result = SNMPMA.Set(gServerId_ID, 123, "ServerDBID")
  SNMPMA.testValue(result.errorStatus, "noAccess(6)") #noAccess(6) means read-only


  #Get table gServersEntry
  gServersEntry_ID = SNMPMA.GetOID('gServersEntry')
  
  #Get table.
  table = SNMPMA.GetTableView(gServersEntry_ID, gServersTableFields)
  print table
  #see how many servers
  SNMPMA.testValue(len(table), 15) 
  #Retrieve first server
  server = table[0]
  print server

  #Retrieve server by Name (field must be uniq)
  objectQAART = table.getByFieldAndValue("gServerName", "QAART")
  print objectQAART
  #check server's property
  SNMPMA.testValue(objectQAART.gServerStatus, 5) 


  #individial servers
  gServerName_ID = SNMPMA.GetOID('gServerName')
  confServ_gServerName_ID = gServerName_ID + CSDBID
  result = SNMPMA.Get(confServ_gServerName_ID)
  SNMPMA.testValue(result.value, "my_confserver") 


  gServerStatus_ID = SNMPMA.GetOID('gServerStatus')
  confServ_gServerStatus_ID = gServerStatus_ID + CSDBID
  result = SNMPMA.Get(confServ_gServerStatus_ID)
  SNMPMA.testValue(result.value, "88") #expect error


  gServerVersion_ID = SNMPMA.GetOID('gServerVersion')
  confServ_gServerVersion_ID = gServerVersion_ID + CSDBID
  result = SNMPMA.Get(confServ_gServerVersion_ID)
  SNMPMA.testValue(result.value, "77", ) #expect error
  
def test2():
  """Waiting snmp trap. Trap is caused by tserver link disconnect"""
  SolutionControlServer.SetDoNotTest(1) # to avoid error in ScsLocalTserver.Start
  ScsLocalTserver.Stop() # to ensure next start
  
  SNMPMA.TrapReceiver.Start() # make sure to start TrapReceiver
  ScsLocalTserver.Start() #produces trap because of no link connection. should be configured properly.
  SNMPMA.WaitTrap(timeout = 3, all = 1) # waits all traps for 3 sec
  SNMPMA.TrapReceiver.Stop() # make sure to stop TrapReceiver
  ScsLocalTserver.Stop() 
  #once more
  SNMPMA.TrapReceiver.Start()
  ScsLocalTserver.Start() 
  SNMPMA.WaitTrap(timeout = 3, all = 1)
  SNMPMA.TrapReceiver.Stop()
  
  SolutionControlServer.SetDoNotTest(0) # reset



  
  